package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.AdminSmBusiFunc;
import cn.com.yusys.yusp.admin.service.AdminSmBusiFuncService;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSmBusiFuncResource
 * @类描述: 业务功能管理接口层
 * @功能描述:
 * @创建人: weimei1@yusys.com.cn
 * @创建时间: 2017-12-12 11:38
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2018宇信科技-版权所有
 */
@RestController
@RequestMapping("/api/adminsmbusifunc")
public class AdminSmBusiFuncResource extends CommonResource<AdminSmBusiFunc, String> {

    @Autowired
    private AdminSmBusiFuncService adminSmBusiFuncService;

    @Override
    protected CommonService getCommonService() {
        return this.adminSmBusiFuncService;
    }

    /**
     * @方法名称:getFuncInfo
     * @方法描述:业务功能信息查询
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/queryfunc")
    public ResultDto<List<Map<String, Object>>> getFuncInfo(QueryModel queryModel) {
        List<Map<String, Object>> list = adminSmBusiFuncService.getFuncInfo(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     * @方法名称: createfunc
     * @方法描述: 新增业务功能信息
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/createfunc")
    public ResultDto<Integer> createFunc(@RequestBody AdminSmBusiFunc adminSmBusiFunc) {
        ResultDto<Integer> resultDto = new ResultDto<>();

        //增加啊后台对URL链接格式的校验 modify at 2018.12.06
        if (!Pattern.matches("^pages\\/([a-zA-Z0-9_]+\\/)+[a-zA-Z0-9_]+(\\?([a-zA-Z0-9_]+=.+))?$", adminSmBusiFunc.getFuncUrl())) {
            resultDto.setMessage("链接格式不正确！");
			resultDto.setCode(2001);//链接格式不正确！
            resultDto.setTotal(0);
            return resultDto;
        }

        int result = this.adminSmBusiFuncService.createFunc(adminSmBusiFunc);
        if (result >= 1) {
            resultDto.setMessage("新增" + adminSmBusiFunc.getFuncName() + "业务功能");
            resultDto.setTotal(result);
        }
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * @方法名称: editFunc
     * @方法描述: 修改业务功能信息
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/editfunc")
    public ResultDto<Integer> editFunc(@RequestBody AdminSmBusiFunc adminSmBusiFunc) {
        ResultDto<Integer> resultDto = new ResultDto<>();
        // ^pages\/([a-zA-Z0-9_]+\/)+[a-zA-Z0-9_]+$
        //增加啊后台对URL链接格式的校验 modify at 2018.12.06  
        if (!Pattern.matches("^pages\\/([a-zA-Z0-9_]+\\/)+[a-zA-Z0-9_]+(\\?([a-zA-Z0-9_]+=.+))?$", adminSmBusiFunc.getFuncUrl())) {
            resultDto.setMessage("链接格式不正确！");
            resultDto.setCode(2001);
            resultDto.setTotal(0);
            return resultDto;
        }

        int result = this.adminSmBusiFuncService.editFunc(adminSmBusiFunc);
        if (result >= 1) {
            resultDto.setMessage("业务功能修改成功！");
            resultDto.setTotal(result);
        }
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * @方法名称: deleteFunc
     * @方法描述: 删除业务功能
     * @参数与返回说明: ids 数据ID主键
     * @算法描述:
     */
    @PostMapping("/deletefunc")
    public ResultDto<Object> deleteFunc(@RequestBody Map<?, ?> ids) {
        String[] idStr = ids.get("id").toString().split(",");
        return adminSmBusiFuncService.deleteFunc(idStr);
    }
}
